import pandas
import pandas as pd
import os

# 设置data文件夹路径
data_dir = os.path.join(os.path.dirname(__file__), 'data')

result = pandas.read_csv('data/time_series_covid19_deaths_global.csv')

# 筛选国家层面数据(Province/State为NaN)并输出全部
national_data = result[result['Province/State'].isna()].copy()  # 显式创建副本

# 确保日期列都是数值类型
date_columns = [col for col in national_data.columns if '/' in col]
national_data[date_columns] = national_data[date_columns].apply(pd.to_numeric, errors='coerce').fillna(0)

# 计算总死亡人数(使用concat避免碎片化)
total_deaths = national_data[date_columns].sum(axis=1)
national_data = pd.concat([national_data, total_deaths.rename('Total_Deaths')], axis=1)

# 输出国家死亡总人数统计
print("\n国家死亡总人数统计:")
print(national_data[['Country/Region', 'Total_Deaths']].sort_values('Total_Deaths', ascending=False))

# 保存结果
output_path = 'C:/Users/hooer/Desktop/python/national_deaths_summary.csv'
national_data.to_csv(output_path, index=False)
print(f"\n数据已成功保存到: {output_path}")



